package com.inffem.gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.inffem.dbif.DBException;
import com.inffem.dbif.DBSubInterface;
import com.inffem.dbseite.DBImpl;

public class NewMainFrame extends JFrame {
	class MyKeyListener implements KeyListener {

		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub

		}

		public void keyReleased(KeyEvent e) {

		}

		public void keyTyped(KeyEvent e) {
			submitEnable();
		}

	}

	static final String BUTTON_SUBMIT = "SubmitButtopn";
	static final String TF_FIRSTNAME = "TfFirstname";
	static final String TF_AUTOR = "TfAutor";
	static final String TF_TITLE = "TfTitle";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		NewMainFrame mainFrame = new NewMainFrame();
		mainFrame.setVisible(true);
	}

	private final JButton buttonSubmit = new JButton("Save Book");
	private JPanel panelButton;
	private JPanel panelContent;
	private JPanel panelMain;
	private final JTextField tfAuthor = new JTextField(80);
	private final JTextField tfFirstname = new JTextField(80);
	private final JTextField tfTitle = new JTextField(80);

	public NewMainFrame() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(500, 300);
		this.setLocationRelativeTo(null);
		this.setTitle("Buecherverwaltung: Eingabe");
		buttonSubmit.setEnabled(false);

		createGUI();
	}

	public void createGUI() {
		panelMain = new JPanel(new GridBagLayout());
		panelContent = new JPanel(new GridBagLayout());
		panelButton = new JPanel(new BorderLayout());

		tfAuthor.setName(TF_AUTOR);
		tfFirstname.setName(TF_FIRSTNAME);
		tfTitle.setName(TF_TITLE);
		JLabel labelAuthor = new JLabel("Autor Nachname*: ");
		JLabel labelfirstName = new JLabel("Autor Vorname*: ");
		JLabel labelTitle = new JLabel("Titel*: ");
		JLabel labelExplanation = new JLabel(
				"(Mit * gekennzeichnete Felder sind Pflichtfelder)");

		tfAuthor.addKeyListener(new MyKeyListener());

		tfTitle.addKeyListener(new MyKeyListener());
		buttonSubmit.setName(BUTTON_SUBMIT);
		buttonSubmit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				insertBook(tfAuthor.getText(), tfFirstname.getText(),
						tfTitle.getText());
			}
		});

		panelContent.add(labelAuthor, new GridBagConstraints(0, 0, 1, 1, 1, 1,
				GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
				new Insets(3, 3, 3, 3), 1, 1));
		panelContent.add(tfAuthor, new GridBagConstraints(1, 0, 1, 1, 100, 1,
				GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
				new Insets(3, 3, 3, 3), 1, 1));
		panelContent.add(labelfirstName, new GridBagConstraints(0, 1, 1, 1, 1,
				1, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
				new Insets(3, 3, 3, 3), 1, 1));
		panelContent.add(tfFirstname, new GridBagConstraints(1, 1, 1, 1, 100,
				1, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
				new Insets(3, 3, 3, 3), 1, 1));
		panelContent.add(labelTitle, new GridBagConstraints(0, 2, 1, 1, 1, 1,
				GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
				new Insets(3, 3, 3, 3), 1, 1));
		panelContent.add(tfTitle, new GridBagConstraints(1, 2, 1, 1, 100, 1,
				GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
				new Insets(3, 3, 3, 3), 1, 1));
		panelContent.add(labelExplanation, new GridBagConstraints(0, 4, 2, 1,
				1, 1, GridBagConstraints.NORTHWEST,
				GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3), 1, 1));

		panelButton.add(buttonSubmit, BorderLayout.EAST);
		panelMain.add(panelContent, new GridBagConstraints(0, 0, 1, 1, 100,
				100, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
				new Insets(20, 20, 20, 20), 1, 1));
		panelMain.add(panelButton, new GridBagConstraints(0, 1, 1, 1, 1, 1,
				GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
				new Insets(20, 20, 20, 20), 1, 1));

		this.add(panelMain);

	}

	DBSubInterface getDBSubInterface() {
		return new DBImpl();
	}

	void insertBook(String autor, String firstname, String titel) {
		DBSubInterface dbInterface = getDBSubInterface();
		try {
			dbInterface.insertBook(autor, firstname, titel);
			openOptionPane("Das Buch wurde erfolgreich gespeichert.",
					"Erfolg!", JOptionPane.PLAIN_MESSAGE);
		} catch (DBException e) {
			openOptionPane(e.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE);
		}
	}

	void openOptionPane(String message, String addtionalText, int errorMessage) {
		JOptionPane.showMessageDialog(panelMain, message, addtionalText,
				JOptionPane.ERROR_MESSAGE);
	}

	void submitEnable() {
		if (tfAuthor.getText().length() > 0 && tfTitle.getText().length() > 0) {
			buttonSubmit.setEnabled(true);
		} else {
			buttonSubmit.setEnabled(false);
		}
	}

}
